#! /bin/sh

#---------------------------------------------------------------------
#                          make-post-install                           
# 
# rewrite package hooks to integrate with erlrc.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" && fws="${FW_ROOT}/share/fw/sh/fw-find.sh"
test -f "fw/sh/fw-find.sh" && fws="fw/sh/fw-find.sh"
test -f "fw.local/sh/fw-find.sh" && fws="fw.local/sh/fw-find.sh"
test ! -z "$fws" || {
  echo "erlang-rebar/make-post-install: fatal: can't locate fw-find.sh" 1>&2
  exit 1
}

. "$fws"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "erlang-rebar/make-post-install" "sh/fw-exec.sh"
fw_source "erlang-rebar/make-post-install" "sh/parse-args.sh"
fw_source "erlang-rebar/make-post-install" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "erlang-rebar/make-post-install" "$@"

validate_args "erlang-rebar/make-post-install" "destdir erlrc libexecdir package" "package"

test "x$erlrc" = x0 || {
  eval `fw_exec "template/erlang-rebar/load-config"`
  
  hookdir="${libexecdir}/fw/$FW_PACKAGE_NAME-$FW_PACKAGE_VERSION"
  
  test -d "${destdir}/${hookdir}" || fatal "erlang-rebar/make-post-install"
  
  test -x "${destdir}/${hookdir}/post-install" && 
  mv "${destdir}/${hookdir}/post-install" \
     "${destdir}/${hookdir}/post-install.orig"
  
  fw_exec "package/$package/make-hook" --hook post-install        \
    --configure "
    which erlrc-start >/dev/null 2>/dev/null
    
    test \$? -ne 0 || \
    erlrc-start \"$FW_ERL_APP_NAME\" \"$FW_PACKAGE_VERSION\" || \
    exit 1
"                                                               \
    --abort_upgrade "
    if \$compare_versions \
      \"$FW_PACKAGE_VERSION\" lt \"\$canon_new_version\"; then
      # aborted package upgrade was to a later version; downgrade to original
      which erlrc-downgrade >/dev/null 2>/dev/null
      test \$? -ne 0 || \
      erlrc-downgrade \"$FW_ERL_APP_NAME\" \"$FW_PACKAGE_VERSION\" \"\$canon_new_version\" || \
      exit 1
    else
      # aborted package upgrade was to an earlier version; upgrade to original
      which erlrc-upgrade >/dev/null 2>/dev/null
      test \$? -ne 0 || \
      erlrc-upgrade \"$FW_ERL_APP_NAME\" \"\$canon_new_version\" \"$FW_PACKAGE_VERSION\" || \
      exit 1
    fi
"                                                               \
    --abort_remove "
    which erlrc-start >/dev/null 2>/dev/null
    
    test \$? -ne 0 || \
    erlrc-start \"$FW_ERL_APP_NAME\" \"$FW_PACKAGE_VERSION\" || \
    exit 1
"                                                               \
    --postamble "
if test -x \"${hookdir}/post-install.orig\"
  then
    set -e
    \"${hookdir}/post-install.orig\" \"\$@\" 
    set +e
  fi
" > "${destdir}/${hookdir}/post-install" &&                     \
  chmod +x "${destdir}/${hookdir}/post-install"
}
